'''
Created on 27/09/2012

@author: Charles Murray
'''

from StructuralModel import *
from DataStream import *

class DataStreamFromFile(object):
    '''
    classdocs
    '''


    def __init__(self, filename, structuralModel):
        '''
        Constructor
        '''
        self.model = StructuralModel(structuralModel)
        self.data = DataStream()
        self.file = open(filename, "r")
        self.content = ""
        if self.model.getRoot().getEnd() == "" and self.model.getRoot().getStart() == "":
            self.content = self.file.read()
            self.data.nextRecord(self.content)
        elif self.model.getRoot().getStart() != "" and self.model.getRoot().getEnd() != "":
            line = self.file.read(1024)
            while line != "":
                #print "Line: {0}".format(line)
                if line.find(self.model.getRoot().getEnd()) == -1:  
                    #print "Line: {0}".format(line)
                    self.content += line
                elif line.find(self.model.getRoot().getEnd()) != -1:
                    #print "Line: {0}".format(line)
                    self.content = line[0:line.index(self.model.getRoot().getEnd(), 1) + len(self.model.getRoot().getEnd())]
                    self.data.nextRecord(self.content)
                    self.content = line[line.index(self.model.getRoot().getEnd(), 1) + len(self.model.getRoot().getEnd()):len(line)]
                line = self.file.read(1024)
        elif self.model.getRoot().getStart() != "":
            line = self.file.read(1024)
            while not line.find(self.model.getRoot().getStart()) and line != "":
                self.content += line
                while line.find(self.model.getRoot().getStart()) != -1 and line != "":
                    #print "Start Line: {0}".format(line)
                    try:
                        self.content = line[0:line.index(self.model.getRoot().getStart(), 1)]
                    except:
                        self.content = line
                    self.data.nextRecord(self.content)
                    try:
                        line = line[line.index(self.model.getRoot().getStart(), 1):len(line)]
                        #print "Try Line: {0}".format(line)
                    except ValueError:
                        line = ""
                    print line
                line = self.file.read(1024)
            #self.data.nextRecord(self.content)
        elif self.model.getRoot().getEnd() != "":
            line = self.file.read(1024)
            print "Start End Line: {0}".format(line)
            while line != "":
                if line.find(self.model.getRoot().getEnd()) == -1:  
                    self.content += line
                print "End Line: {0}".format(line)
                while line.find(self.model.getRoot().getEnd()) != -1:
                    self.content = line[0:(line.index(self.model.getRoot().getEnd()) + len(self.model.getRoot().getEnd()))]
                    self.data.nextRecord(self.content)
                    line = line[line.index(self.model.getRoot().getEnd()) + len(self.model.getRoot().getEnd()):len(line)]
                line = self.file.read(1024)
                

    def getDataStream(self):
        return self.data
        